package gnss

import (
	"dgo/goutils/utils"
	"errors"
	"fmt"
	"strings"
	"time"
)

const (
	LIGHT_SPEED float64 = 299792458.0 /* speed of light (m/s) */
	CIGHT_SPEED float64 = 299792458.0
	RANGE_MS    float64 = LIGHT_SPEED * 0.001 /* range in 1 ms */
	PI          float64 = 3.1415926535897932  /* pi */
	D2R         float64 = (PI / 180.0)        /* deg to rad */
	R2D         float64 = (180.0 / PI)        /* rad to deg */
	SC2RAD      float64 = 3.1415926535898     /* semi-circle to radian (IS-GPS) */
	OMGE        float64 = 7.2921151467e-5     /* earth angular velocity (IS-GPS) (rad/s) */

	PRUNIT_GPS float64 = 299792.458 /* rtcm ver.3 unit of gps pseudorange (m) */
	PRUNIT_GLO float64 = 599584.916 /* rtcm ver.3 unit of glonass pseudorange (m) */

	MAX_OBS_N int = 64

	P2_3  float64 = 0.125                 /* 2^-3 */
	P2_4  float64 = 0.0625                /* 2^-4 */
	P2_5  float64 = 0.03125               /* 2^-5 */
	P2_6  float64 = 0.015625              /* 2^-6 */
	P2_8  float64 = 0.00390625            /* 2^-8 */
	P2_9  float64 = 0.001953125           /* 2^-9 */
	P2_10 float64 = 0.0009765625          /* 2^-10 */
	P2_11 float64 = 4.882812500000000e-04 /* 2^-11 */
	P2_15 float64 = 3.051757812500000e-05 /* 2^-15 */
	P2_17 float64 = 7.629394531250000e-06 /* 2^-17 */
	P2_19 float64 = 1.907348632812500e-06 /* 2^-19 */
	P2_20 float64 = 9.536743164062500e-07 /* 2^-20 */
	P2_21 float64 = 4.768371582031250e-07 /* 2^-21 */
	P2_23 float64 = 1.192092895507810e-07 /* 2^-23 */
	P2_24 float64 = 5.960464477539063e-08 /* 2^-24 */
	P2_27 float64 = 7.450580596923828e-09 /* 2^-27 */
	P2_29 float64 = 1.862645149230957e-09 /* 2^-29 */
	P2_30 float64 = 9.313225746154785e-10 /* 2^-30 */
	P2_31 float64 = 4.656612873077393e-10 /* 2^-31 */
	P2_32 float64 = 2.328306436538696e-10 /* 2^-32 */
	P2_33 float64 = 1.164153218269348e-10 /* 2^-33 */
	P2_34 float64 = 5.820766091346741e-11 /* 2^-34 */
	P2_35 float64 = 2.910383045673370e-11 /* 2^-35 */
	P2_38 float64 = 3.637978807091710e-12 /* 2^-38 */
	P2_39 float64 = 1.818989403545856e-12 /* 2^-39 */
	P2_40 float64 = 9.094947017729280e-13 /* 2^-40 */
	P2_43 float64 = 1.136868377216160e-13 /* 2^-43 */
	P2_44 float64 = 5.684341886080802e-14 /* 2^-44 */
	P2_46 float64 = 1.421085471520200e-14
	P2_48 float64 = 3.552713678800501e-15 /* 2^-48 */
	P2_50 float64 = 8.881784197001252e-16 /* 2^-50 */
	P2_55 float64 = 2.775557561562891e-17 /* 2^-55 */
	P2_57 float64 = 6.938893903907228e-18 /* 2^-57 */
	P2_59 float64 = 1.734723475976810e-18
	P2_66 float64 = 1.3552527156068805e-20 /* 2^-66 */

	P211   float64 = 2048  /* 2^11 */
	P214   float64 = 16384 /* 2^14 */
	P216   float64 = 65536 /* 2^16 */
	P10_7  float64 = 1e-7  // 10^-7
	P10_10 float64 = 1e-10 // 10^-10

	P2_10_RANGE_MS = P2_10 * RANGE_MS

	//navigation system
	NAVI_SYS_MAX byte = 7
	SATID_MAX    byte = 64

	FREQ_BD_BAND_TYPE_B1I  byte = 1
	FREQ_BD_BAND_TYPE_B2I  byte = 2
	FREQ_BD_BAND_TYPE_B3I  byte = 3
	FREQ_BD_BAND_TYPE_B1C  byte = 4
	FREQ_BD_BAND_TYPE_B2A  byte = 5
	FREQ_BD_BAND_TYPE_B2B  byte = 6
	FREQ_BD_BAND_TYPE_B3C  byte = 7
	FREQ_BD_BAND_TYPE_B2AB byte = 8

	// frequency 1191.795
	FREQ_BD_B1I  float64 = 1.561098e9 /* BeiDou B1 frequency (Hz) */
	FREQ_BD_B2I  float64 = 1.20714e9  /* BeiDou B2 frequency (Hz) */
	FREQ_BD_B3I  float64 = 1.26852e9  /* BeiDou B3 frequency (Hz) */
	FREQ_BD_B1C  float64 = 1.57542e9  /* BeiDou B1 frequency (Hz) (iGMAS) */
	FREQ_BD_B2A  float64 = 1.17645e9  /* BeiDou B2 frequency (Hz) (iGMAS) */
	FREQ_BD_B2B  float64 = 1.20714e9  /* BeiDou B3 frequency (Hz) (iGMAS)*/
	FREQ_BD_B3C  float64 = 1.26852e9  /* BeiDou B3 frequency (Hz) (iGMAS, 新版本不再使用)*/
	FREQ_BD_B2AB float64 = 1.191795e9

	//Frequency Band name
	FREQ_GPS_TYPE_L1 byte    = 1
	FREQ_GPS_TYPE_L2 byte    = 2
	FREQ_GPS_TYPE_L5 byte    = 5
	FREQ_GPS_L1      float64 = 1.57542e9  /* L1/E1  frequency (Hz)  FREQ1 */
	FREQ_GPS_2       float64 = 1.22760e9  /* L2     frequency (Hz)  FREQ2 */
	FREQ_GPS_5       float64 = 1.17645e9  /* L5/ E5a frequency (Hz) FREQ5 */
	FREQ_GPS_6       float64 = 1.27875e9  /* E6/ LEX frequency (Hz) FREQ6*/
	FREQ_GPS_7       float64 = 1.20714e9  /* E5b    frequency (Hz)  FREQ7*/
	FREQ_GPS_8       float64 = 1.191795e9 /* E5a+b  frequency (Hz) FREQ8*/

	// GAL
	FREQ_GAL_TYPE_E1  byte    = 1
	FREQ_GAL_TYPE_E5  byte    = 5
	FREQ_GAL_TYPE_E5A byte    = 51
	FREQ_GAL_TYPE_E5B byte    = 52
	FREQ_GAL_TYPE_E6  byte    = 6
	FREQ_E1           float64 = FREQ_GPS_L1
	FREQ_E5A          float64 = FREQ_GPS_5
	FREQ_E6           float64 = FREQ_GPS_6
	FREQ_E5B          float64 = FREQ_GPS_7
	FREQ_E5AB         float64 = FREQ_GPS_8

	//Frequency Band name
	FREQ1_GLO_TYPE_G1  byte    = 1
	FREQ1_GLO_TYPE_G1A byte    = 11
	FREQ1_GLO_TYPE_G2  byte    = 2
	FREQ1_GLO_TYPE_G2A byte    = 21
	FREQ1_GLO_TYPE_G3  byte    = 3
	FREQ1_GLO          float64 = 1.60200e9  /* GLONASS G1 base frequency (Hz) */
	DFRQ1_GLO          float64 = 0.56250e6  /* 9/16 GLONASS G1 bias frequency (Hz/n) */
	FREQ2_GLO          float64 = 1.24600e9  /* GLONASS G2 base frequency (Hz) */
	DFRQ2_GLO          float64 = 0.43750e6  /* 7/16 GLONASS G2 bias frequency (Hz/n) */
	FREQ3_GLO          float64 = 1.202025e9 /* GLONASS G3 frequency (Hz) */

	FREQ_SBAS_TYPE_L1 byte    = 1
	FREQ_SBAS_TYPE_L5 byte    = 5
	FREQ_SBAS_L1      float64 = FREQ_GPS_L1
	FREQ_SBAS_L5      float64 = FREQ_GPS_5
)

var (
	SYS_UNKOWN byte = 0
	// 匹配千寻输出的顺序
	//SYS_GLO             byte = 1 // GLONASS
	//SYS_BD              byte = 2 // BeiDou
	//SYS_GPS             byte = 3 // GPS
	//SYS_GAL             byte = 4 // GAL
	//SYS_SBS             byte = 5 // SBAS
	//SYS_QZS             byte = 6 // QZSS
	//NAV_SYS_NAME             = []string{"", "GLONASS", "BeiDou", "GPS", "GALILEO", "SBAS", "QZSS"}
	//GNSS_SYS_RINEX_CHAR      = []string{"_", "R", "C", "G", "E", "S", "J"} //Frequency Band name

	// 华测顺序
	SYS_BD              byte = 1 // BeiDou
	SYS_GLO             byte = 2 // GLONASS
	SYS_GAL             byte = 3 // GAL
	SYS_QZS             byte = 4 // QZSS
	SYS_GPS             byte = 5 // GPS
	SYS_SBS             byte = 6 // SBAS
	SYS_WLNAV           byte = 7 //  低轨
	NAV_SYS_NAME             = []string{"", "BeiDou", "GLONASS", "GALILEO", "QZSS", "GPS", "SBAS", "WLNAV"}
	GNSS_SYS_RINEX_CHAR      = []string{"_", "C", "R", "E", "J", "G", "S", "W"} //Frequency Band name

	// SYS_GPS .... SYS_GAL   // RINEX 3.04.IGS.RTCM.doc page 34 search:Constellation Identifiers

	// GPS时间比UTC时间快的秒数
	GPS_FASTER_THAN_UTC float64 = 18

	URA_VAL = []float64{2.4, 3.4, 4.85, 6.85, 9.65, 13.65, 24.00, 48.00, 96.00, 192.00, 384.00,
		768.00, 1536.00, 3072.00, 6144.00, 8192.00,
	}

	// GLO卫星FRQ 计算波长 prn -1
	// before
	//GLO_FRQ = []float64{
	//	1, -4, 5, 6, 1, -4, 5, 6,
	//	-6, -7, 0, -1, -2, -7, 0, -1,
	//	4, -3, 3, 2, 4, -3, 3, 2, -5, 7,
	//}

	// update 2023-04-25 09:19:41 徐文杰 发现R09相位不对
	// http://www.csno-tarc.cn/glonass/constellation

	GLO_FRQ = []float64{
		1, -4, 5, 6, 1, -4, 5, 6, /* Plane 1: orb. slot 1~8   */
		-2, -7, 0, -1, -2, -7, 0, -1, /* Plane 2: orb. slot 9~16  */
		4, -3, 3, 2, 4, -3, 3, 2, /* Plane 2: orb. slot 17~24 */
	}

	// 北斗卫星, id->prn
	BDS_PRN_MAP = []string{"", "C01", "C02", "C03", "C04", "C05", "C06", "C07", "C08", "C09", "C10",
		"C11", "C12", "C13", "C14", "C15", "C16", "C17", "C18", "C19", "C20",
		"C21", "C22", "C23", "C24", "C25", "C26", "C27", "C28", "C29", "C30",
		"C31", "C32", "C33", "C34", "C35", "C36", "C37", "C38", "C39", "C40",
		"C41", "C42", "C43", "C44", "C45", "C46", "C47", "C48", "C49", "C50",
		"C51", "C52", "C53", "C54", "C55", "C56", "C57", "C58", "C59", "C60",
		"C61", "C62", "C63", "C64", "C65", "C66", "C67",
	}

	GPS_PRN_MAP = []string{"", "G01", "G02", "G03", "G04", "G05", "G06", "G07", "G08", "G09", "G10",
		"G11", "G12", "G13", "G14", "G15", "G16", "G17", "G18", "G19", "G20",
		"G21", "G22", "G23", "G24", "G25", "G26", "G27", "G28", "G29", "G30",
		"G31", "G32", "G33", "G34", "G35", "G36", "G37", "G38", "G39", "G40",
	}

	GAL_PRN_MAP = []string{"", "E01", "E02", "E03", "E04", "E05", "E06", "E07", "E08", "E09", "E10",
		"E11", "E12", "E13", "E14", "E15", "E16", "E17", "E18", "E19", "E20",
		"E21", "E22", "E23", "E24", "E25", "E26", "E27", "E28", "E29", "E30",
		"E31", "E32", "E33", "E34", "E35", "E36", "E37", "E38", "E39", "E40",
	}

	GLO_PRN_MAP = []string{"", "R01", "R02", "R03", "R04", "R05", "R06", "R07", "R08", "R09", "R10",
		"R11", "R12", "R13", "R14", "R15", "R16", "R17", "R18", "R19", "R20",
		"R21", "R22", "R23", "R24", "R25", "R26", "R27", "R28", "R29", "R30",
		"R31", "R32", "R33", "R34", "R35", "R36", "R37", "R38", "R39", "R40",
	}

	QZSS_PRN_MAP = []string{"", "J01", "J02", "J03", "J04", "J05", "J06", "J07", "J08", "J09", "J10",
		"J11", "J12", "J13", "J14", "J15", "J16", "J17", "J18", "J19", "J20",
		"J21", "J22", "J23", "J24", "J25", "J26", "J27", "J28", "J29", "J30",
		"J31", "J32", "J33", "J34", "J35", "J36", "J37", "J38", "J39", "J40",
	}

	SBAS_PRN_MAP = []string{"", "S01", "S02", "S03", "S04", "S05", "S06", "S07", "S08", "S09", "S10",
		"S11", "S12", "S13", "S14", "S15", "S16", "S17", "S18", "S19", "S20",
		"S21", "S22", "S23", "S24", "S25", "S26", "S27", "S28", "S29", "S30",
		"S31", "S32", "S33", "S34", "S35", "S36", "S37", "S38", "S39", "S40",
	}

	// BDS_SIGN_MAP[iSign -1]  :iSign(1-32)
	//  表 4-25 BDS 信号映射关系 (20180601-iGMAS-T11TAC008-03V4.0_RTCM协议.pdf) RTCM (BeiDou Signal ID Mapping )
	//   20180205-iGMAS-T11TAC008-03V4.0_RTCM协议.pdf(进行了定义, 5,6,7 和星星通板卡使用了)
	// Table 3.5-108 BeiDou Signal ID Mapping

	/*
		千寻客服：2021-09-26 09:45:27
		b1c 31 b2a 23

		华测：
		北斗B1C放在频点索引29上，B2a放在频点索引21上, 这个我们是从0开始计算
	*/
	BDS_SIGN_MAP = []string{
		"",               // 1
		"2I", "2Q", "2X", // 2, 3,4       (B1)
		"1D", "1P", "1X", // 5, 6, 7      (B1C) RINEX文档中, 但是没有具体编号 // UB4B0 非标
		"6I", "6Q", "6X", // 8, 9, 10     (B3)
		"", "", "", // 11-13
		"7I", "7Q", "7X", // 14, 15, 16   (B2)
		"5D", "5P", "5X", // 17, 18, 19   (B2a)    // 非标(IGMAS)

		"7D", "7P", // 20, 21    (B2b)    // 非标

		"5D", "5P", "5X", // 22, 23, 24  B2a
		"7D",             // 25   (B2b)
		"8D", "8P", "8X", // 26, 27, 28   (B2a + B2b)   // 非标
		"",               // 29
		"1D", "1P", "1X", // 30, 31, 32   (B1C)

	}

	RTCM_BDS_SIGN_B1C_1D byte = 30
	RTCM_BDS_SIGN_B1C_1P byte = 31
	RTCM_BDS_SIGN_B1C_1X byte = 32

	RTCM_BDS_SIGN_B2B_7P byte = 21 // 非标

	RTCM_BDS_SIGN_B3_6I byte = 8

	RTCM_BDS_SIGN_B2A_5D byte = 22
	RTCM_BDS_SIGN_B2A_5P byte = 23
	RTCM_BDS_SIGN_B2A_5X byte = 24
	RTCM_BDS_SIGN_B2B_7D byte = 25

	RTCM_BDS_SIGN_B2AB_8P byte = 26

	// GPS_SIGN_MAP[iSign -1]  :iSign(1-32)
	// Table 3.5-91 GPS Signal ID mapping
	GPS_SIGN_MAP = []string{"",
		"1C", "1P", "1W", // 2, 3, 4    L1
		"", "", "", //
		"2C", "2P", "2W", // 8, 9, 10   L2
		"", "", "", "", // 11-14
		"2S", "2L", "2X", // 15-17     L2
		"", "", "", "", // 18-21
		"5I", "5Q", "5X", // 22-24     L5
		"", "", "", "", "", // 25 -29
		"1S", "1L", "1X", //30-32    // L1
	}

	// GAL_SIGN_MAP[iSign -1]  :iSign(1-32) Galileo
	// Table 3.5-99 Galileo Signal ID mapping
	GAL_SIGN_MAP = []string{"",
		"1C", "1A", "1B", // 2-4    E1
		"1X", "1Z", // 5,6        E1
		"",                           // 7
		"6C", "6A", "6B", "6X", "6Z", // 8-12    E6
		"",               // 13
		"7I", "7Q", "7X", // 14-16     // E5B
		"",               // 17
		"8I", "8Q", "8X", // 18-20    // E5(A+B)
		"",               // 21
		"5I", "5Q", "5X", // 22-24    // E5A
		"",                         // 25
		"", "", "", "", "", "", "", //26-32
	}

	// GLO_SIGN_MAP[iSign -1]  :iSign(1-32) Galileo
	// Table 3.5-96 GLONASS Signal ID mapping
	GLO_SIGN_MAP = []string{
		"", "1C", "1P", // 1,2,3
		"", "", "", // 4, 5,6,
		"", "2C", "2P", // 7,8,9
		"",
		"", "", "",
		"", "", "", "", "", "", "", "", "", "", "", "", // 14-25
		"", "", "", "", "", "", "", //26-32
	}

	// QZSS Signal ID Mapping
	QZS_SIGN_MAP = []string{"",
		"1C", "", "", // 2, 3, 4    L1
		"", "", "", "", // 5,6,7, 8
		"6S", "6L", "6X", // 9, 10 11    LEX L6
		"", "", "", // 12-14
		"2S", "2L", "2X", // 15-17     L2
		"", "", "", "", // 18-21
		"5I", "5Q", "5X", // 22-24     L5
		"", "", "", "", "", // 25 -29
		"1S", "1L", "1X", //30-32    // L1
	}

	// Table 3.5-102 SBAS Signal ID Mapping
	SBAS_SIGN_MAP = []string{"",
		"1C", "", "", // 2, 3, 4    L1
		"", "", "", "", // 5,6,7, 8
		"", "", "", // 9, 10 11
		"", "", "", // 12-14
		"", "", "", // 15-17     L2
		"", "", "", "", // 18-21
		"5I", "5Q", "5X", // 22-24     L5
		"", "", "", "", "", // 25 -29
		"", "", "", //30-32    // L1
	}

	BD_START_TIME  = time.Date(2006, 1, 1, 0, 0, 0, 0, time.UTC)
	GPS_START_TIME = time.Date(1980, 1, 6, 0, 0, 0, 0, time.UTC)
	GAL_START_TIME = time.Date(1999, 8, 22, 0, 0, 0, 0, time.UTC)
	GLO_START_TIME = time.Date(1999, 8, 22, 0, 0, 0, 0, time.UTC)

	// GPS时间与当前时间的差值  当前时间-GPST的插值, 单位(s)
	GPST_SPAN float64 = 0
)

const (
	// GPS 最大：32 rtklib中预留是32
	MAX_GPS_PRN_ID byte = 32
	MIN_GPS_PRN_ID byte = 1

	MAX_BD_PRN_ID = 67
	MIN_BD_PRN_ID = 1

	MAX_GAL_PRN_ID = 40
	MIN_GAL_PRN_ID = 1

	MAX_GLO_PRN_ID = 27
	MIN_GLO_PRN_ID = 1

	// 120~158 183~187
	MIN_SBS_PRN_ID = 120
	MAX_SBS_PRN_ID = 187

	// UTC 转 GPS时间时需要 增加的秒数, <GLONASS 在转换时需要用到跳秒>
	LEAP_SECONDS = 18
)

func GetStartTime(nsys byte) time.Time {
	if nsys == SYS_BD {
		return BD_START_TIME
	} else if nsys == SYS_GAL {
		return GAL_START_TIME
	} else {
		return GPS_START_TIME
	}
}

func GetCycleWeek(nsys byte) int32 {
	if nsys == SYS_BD {
		return 8192
	} else if nsys == SYS_GAL {
		return 4096
	} else {
		return 1024
	}
}

func GetNavSys(strNaviCode string) byte {
	if strNaviCode == "C" {
		return SYS_BD
	} else if strNaviCode == "G" {
		return SYS_GPS
	} else if strNaviCode == "E" {
		return SYS_GAL
	} else if strNaviCode == "R" {
		return SYS_GLO
	} else if strNaviCode == "S" {
		return SYS_SBS
	} else if strNaviCode == "J" {
		return SYS_QZS
	}

	return SYS_UNKOWN
}

func GetNavSysStr(strSys string) byte {
	if strSys == "BDS" {
		return SYS_BD
	} else if strSys == "GPS" {
		return SYS_GPS
	} else if strSys == "GAL" {
		return SYS_GAL
	} else if strSys == "GLO" {
		return SYS_GLO
	} else if strSys == "SBAS" || strSys == "SBS" {
		return SYS_SBS
	} else if strSys == "QZS" || strSys == "QZSS" {
		return SYS_QZS
	}

	return SYS_UNKOWN
}

// / 信号->RINEX-CODE
func SigiRinexCode(satsys byte, sigv byte) string {
	sigv -= 1
	if satsys == SYS_GPS {
		return GPS_SIGN_MAP[sigv]
	} else if satsys == SYS_BD {
		return BDS_SIGN_MAP[sigv]
	} else if satsys == SYS_GAL {
		return GAL_SIGN_MAP[sigv]
	} else if satsys == SYS_GLO {
		return GLO_SIGN_MAP[sigv]
	} else if satsys == SYS_QZS {
		return QZS_SIGN_MAP[sigv]
	} else if satsys == SYS_SBS {
		return SBAS_SIGN_MAP[sigv]
	} else {
		return "NN"
	}
}

// / 信号->频段ID
func SigiBrandID(satsys byte, sigv byte) string {
	if satsys == SYS_GPS {
		if sigv <= 1 {
			return "UN"
		} else if sigv >= 2 && sigv <= 4 { //2..4   L1
			return "L1"
		} else if sigv == 8 { //2..4   L2
			return "L2"
		} else if sigv >= 9 && sigv <= 10 { //9..10 L2
			return "L2"
		} else if sigv >= 15 && sigv <= 17 { //15..17 L2
			return "L2"
		} else if sigv >= 22 && sigv <= 24 { //22..24 L5
			return "L5"
		} else if sigv >= 30 && sigv <= 32 { //30..32 L1
			return "L1"
		}
	} else if satsys == SYS_BD {
		if sigv <= 1 { // 未知
			return "UN"
		} else if sigv >= 2 && sigv <= 4 { //2..4   I, Q, I+Q  B1(一期)
			return "B1"
		} else if sigv <= 7 { // 5..7   B1C  (应该是保留的)  20180205-iGMAS-T11TAC008-03V4.0_RTCM协议.pdf(进行了定义)
			return "B1C"
		} else if sigv <= 10 { // 8...10
			return "B3"
		} else if sigv <= 13 { // 11..13
			return "B3C"
		} else if sigv <= 16 { // 14..16
			return "B2"
		} else if sigv <= 19 { // 17..19
			return "B2a"
		} else if sigv <= 22 { // 20..22
			return "B2b"
		} else if sigv <= 25 { // 23..25    B2(B2a+B2b)
			return "UN"
		} else if sigv >= 26 && sigv <= 31 { // 26..31
			return "B1C"
		}
	} else if satsys == SYS_GAL {
		if sigv <= 1 {
			return "UN"
		} else if sigv >= 2 && sigv <= 6 { //2..6   E1
			return "E1"
		} else if sigv >= 8 && sigv <= 12 { //8..12 E6
			return "E6"
		} else if sigv >= 14 && sigv <= 16 { //14..16 E5B
			return "E5B"
		} else if sigv >= 18 && sigv <= 20 { //18..20 E5(A+B)
			return "E5AB"
		} else if sigv >= 22 && sigv <= 24 { //22..24 E5A
			return "E5A"
		}
		return "UN"
	} else if satsys == SYS_GLO {
		if sigv <= 1 {
			return "UN"
		} else if sigv >= 2 && sigv <= 3 { //2..3   G1
			return "G1"
		} else if sigv >= 8 && sigv <= 9 { //8..9  G2
			return "G2"
		}
		return "UN"
	} else {
		return "UN"
	}

	return "UN"
}

/*
**

	根据信号字符串返回信号对应的索引值
	BDS 的 2I 对应的值是1
*/
func GetISignCode(sSign string, navisys byte) int {
	var signMap []string = nil
	if navisys == SYS_GPS {
		signMap = GPS_SIGN_MAP
	} else if navisys == SYS_BD {
		signMap = BDS_SIGN_MAP
	} else if navisys == SYS_GAL {
		signMap = GAL_SIGN_MAP
	} else if navisys == SYS_GLO {
		signMap = GLO_SIGN_MAP
	}

	if signMap == nil {
		return -1
	}

	for idx, v := range signMap {
		if sSign == v {
			return idx + 1
		}
	}
	return -1
}

func ParseSigCodeText(navisys byte, sigtext, sep string) []byte {
	if len(sigtext) == 0 {
		return nil
	}
	strs := strings.Split(sigtext, sep)
	rval := make([]byte, 0, len(strs))
	for i := 0; i < len(strs); i++ {
		str := utils.Trim(strs[i])
		if len(str) > 0 {
			v := SigRinexID2SigV(navisys, str)
			if v > 0 {
				rval = append(rval, v)
			}
		}

	}
	return rval
}

func SigRinexID2SigV(navisys byte, sigtext string) byte {
	var sigtextlst []string
	if navisys == SYS_BD {
		switch sigtext {
		case "5D":
			return RTCM_BDS_SIGN_B2A_5D
		case "5P":
			return RTCM_BDS_SIGN_B2A_5P
		case "5X":
			return RTCM_BDS_SIGN_B2A_5X
		case "7D":
			return RTCM_BDS_SIGN_B2B_7D
		case "8P":
			return RTCM_BDS_SIGN_B2AB_8P

		case "1P":
			return RTCM_BDS_SIGN_B1C_1P // 优先使用
		case "1D":
			return RTCM_BDS_SIGN_B1C_1D // 优先使用
		case "1X":
			return RTCM_BDS_SIGN_B1C_1X // 优先使用
		}
		sigtextlst = BDS_SIGN_MAP
	} else if navisys == SYS_GPS {
		sigtextlst = GPS_SIGN_MAP
	} else if navisys == SYS_GLO {
		sigtextlst = GLO_SIGN_MAP
	} else if navisys == SYS_GAL {
		sigtextlst = GAL_SIGN_MAP
	} else if navisys == SYS_QZS {
		sigtextlst = QZS_SIGN_MAP
	} else if navisys == SYS_SBS {
		sigtextlst = SBAS_SIGN_MAP
	}
	if len(sigtextlst) > 0 {
		for i := 0; i < len(sigtextlst); i++ {
			if sigtextlst[i] == sigtext {
				return byte(i + 1)
			}
		}
	}
	return 0
}

func GetSignCode(isign byte, navisys byte) string {
	if navisys == SYS_GPS || navisys == SYS_SBS {
		return GPS_SIGN_MAP[isign-1]
	} else if navisys == SYS_BD {
		return BDS_SIGN_MAP[isign-1]
	} else if navisys == SYS_GAL {
		return GAL_SIGN_MAP[isign-1]
	} else if navisys == SYS_GLO {
		return GLO_SIGN_MAP[isign-1]
	} else if navisys == SYS_QZS {
		return QZS_SIGN_MAP[isign-1]
	}
	return "NNN"
}

func CheckSatIdInvalid(satid byte, navisys byte) error {
	if navisys == SYS_GPS {
		if satid >= MIN_GPS_PRN_ID || satid <= MAX_GPS_PRN_ID {
			return nil
		} else if satid >= MIN_SBS_PRN_ID || satid <= MAX_SBS_PRN_ID { //Hemi解码使用了
			return nil
		} else {
			return fmt.Errorf("错误的GPS卫星号:%d:%d", navisys, satid)
		}
		return nil
	} else if navisys == SYS_BD {
		if satid < MIN_BD_PRN_ID || satid > MAX_BD_PRN_ID {
			return errors.New(fmt.Sprintf("错误的BD卫星号:%d", satid))
		}

		return nil
	} else if navisys == SYS_GAL {
		if satid < MIN_GAL_PRN_ID || satid > MAX_GAL_PRN_ID {
			return errors.New(fmt.Sprintf("错误的GAL卫星号:%d", satid))
		}
		return nil
	} else if navisys == SYS_GLO {
		if satid < MIN_GLO_PRN_ID || satid > MAX_GLO_PRN_ID {
			return errors.New(fmt.Sprintf("错误的GLONASS卫星号:%d", satid))
		}
		return nil
	} else if navisys == SYS_SBS {
		if satid < MIN_SBS_PRN_ID || satid > MAX_SBS_PRN_ID {
			return errors.New(fmt.Sprintf("错误的SBS卫星号:%d", satid))
		}
		return nil
	} else if navisys == SYS_QZS {
		if satid < 1 || satid > 64 {
			return errors.New(fmt.Sprintf("错误的QZS卫星号:%d", satid))
		}
		return nil
	} else if navisys == SYS_SBS {
		if satid < 1 || satid > 64 {
			return errors.New(fmt.Sprintf("错误的SBS卫星号:%d", satid))
		}
		return nil
	}
	return errors.New(fmt.Sprintf("未知的系统[%d]", navisys))
}

func GetPrnStrCode(satid byte, navisys byte) string {
	if navisys == SYS_GPS {
		if satid >= MIN_GPS_PRN_ID && satid <= MAX_GPS_PRN_ID {
			return GPS_PRN_MAP[satid]
		} else if satid >= MIN_SBS_PRN_ID && satid <= MAX_SBS_PRN_ID { // Hemi解码使用了
			return fmt.Sprintf("S%.2d", satid-100)
		}
		return "GNN"
	} else if navisys == SYS_SBS {
		return fmt.Sprintf("S%.2d", satid)
	} else if navisys == SYS_BD {
		if satid < MIN_BD_PRN_ID || satid > MAX_BD_PRN_ID {
			//utils.Log.Warn(fmt.Sprintf("错误的BD卫星号:%d, %s", satid, utils.GetCallerInfo(1)))
			return "CNN"
		}

		//if satid > 30 {
		//	utils.Log.Warn(fmt.Sprintf("牛逼卫星号:%d, %s", satid, utils.GetCallerInfo(1)))
		//}

		return BDS_PRN_MAP[satid]
	} else if navisys == SYS_GAL {
		if satid < MIN_GAL_PRN_ID || satid > MAX_GAL_PRN_ID {
			//utils.Log.Warn(fmt.Sprintf("错误的GAL卫星号:%d, %s", satid, utils.GetCallerInfo(1)))
			return "ENN"
		}
		return GAL_PRN_MAP[satid]
	} else if navisys == SYS_GLO {
		if satid < MIN_GLO_PRN_ID || satid > MAX_GLO_PRN_ID {
			//utils.Log.Warn(fmt.Sprintf("错误的GLONASS卫星号:%d, %s", satid, utils.GetCallerInfo(1)))
			return "RNN"
		}
		return GLO_PRN_MAP[satid]
	} else if navisys == SYS_QZS {
		if satid < MIN_GPS_PRN_ID || satid > MAX_GPS_PRN_ID {
			return "JNN"
		}
		return QZSS_PRN_MAP[satid]
	} else if navisys == SYS_SBS {
		//if satid < MIN_GPS_PRN_ID || satid > MAX_GPS_PRN_ID {
		//	return "JNN"
		//}
		return SBAS_PRN_MAP[satid]
	}
	return "NNN"
}
